/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_NODE_ANIMATOR_H
#define IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_NODE_ANIMATOR_H

#include "imageknife_node_internal.h"
namespace ImageKnifePro {
class ImageKnifeNodeAnimator : public ImageKnifeNodeInternal {
public:
    ImageKnifeNodeAnimator(std::shared_ptr<ImageKnifeOption> option, std::shared_ptr<AnimatorOption> animatorOption)
        : ImageKnifeNodeInternal(option, GetNodeAPI()->createNode(ARKUI_NODE_IMAGE_ANIMATOR)), option_(animatorOption)
    {
        SetPercentHeight(1);
        SetPercentWidth(1);
        RegisterAnimatorEvent();
        SetAnimatorAttribute();
    }

    ~ImageKnifeNodeAnimator() override
    {
        UnRegisterAnimatorEvent();
        ImageKnifeNodeAnimator::RemoveDisplayedImage();
        imageData_ = nullptr;
    }

    bool Display(std::shared_ptr<ImageDataCacheInternal> data, bool isMainSrc) override
    {
        IMAGE_KNIFE_CHECK_RETURN(data == nullptr, false);
        if (isMainSrc) {
            imageData_ = data;
        }
        if (data->GetFrameInfoList() != nullptr) {
            if (!isMainSrc && (option_->state == ARKUI_ANIMATION_STATUS_STOPPED ||
                option_->state == ARKUI_ANIMATION_STATUS_PAUSED)) {
                // 占位图将状态设置为Initial，再设置图片资源，规避动图不能重复设置资源的问题
                ArkUI_NumberValue number = {.i32 = ARKUI_ANIMATION_STATUS_INITIAL};
                ArkUI_AttributeItem statusItem = {&number, 1};
                GetNodeAPI()->setAttribute(handle, NODE_IMAGE_ANIMATOR_STATE, &statusItem);
            }

            ArkUI_AttributeItem item = {.size = static_cast<int32_t>(data->GetFrameCount()),
                                        .object = data->GetFrameInfoList()};
            GetNodeAPI()->setAttribute(handle, NODE_IMAGE_ANIMATOR_IMAGES, &item);
            return true;
        }

        return false;
    }

    // Animator 暂没有Draggable设计,进行空实现override
    void Draggable(bool draggable) override
    {
    }

    // Animator 暂没有syncLoad设计,进行空实现override
    void SyncLoad(bool syncLoad) override
    {
    }

    bool IsAnimator() const override
    {
        return true;
    }

    void OnDerivedNodeEvent(ArkUI_NodeEvent *event) override;

    void UpdateAnimatorOption(std::shared_ptr<AnimatorOption> option) override
    {
        UpdateAnimatorNodeEvent(option);
        option_ = option;

        SetAnimatorAttribute();
    }
private:
    std::shared_ptr<AnimatorOption> option_ = nullptr;
    std::shared_ptr<ImageDataCacheInternal> imageData_ = nullptr;

    void RegisterAnimatorEvent();
    void UnRegisterAnimatorEvent();
    void RemoveDisplayedImage() override;

    void SetAnimatorAttribute();
    void UpdateAnimatorNodeEvent(std::shared_ptr<AnimatorOption> option);
};

} // end of namespace

#endif // IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_NODE_ANIMATOR_H
